package com.zsjt.yysapi.core.service.impl;

import com.zsjt.yysapi.common.handler.mybatisPlus.CustomQueryWrapper;
import com.zsjt.yysapi.core.pojo.Category;
import com.zsjt.yysapi.core.mapper.CategoryMapper;
import com.zsjt.yysapi.core.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 分类 服务实现类
 * </p>
 *
 * @author zsq
 * @since 2021-07-12
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 列表
     *
     * @param category
     * @return
     */
    @Override
    public List<Category> list(Category category) {
        CustomQueryWrapper<Category> wrapper = new CustomQueryWrapper<>();
        wrapper.eq("type", category.getType());
        List<Category> list = categoryMapper.selectList(wrapper);
        return buildCategoryTree(list);
    }

    /**
     * 构建树结构
     *
     * @param categorys 分类列表
     * @return 树结构列表
     */
    public List<Category> buildCategoryTree(List<Category> categorys) {
        List<Category> returnList = new ArrayList<>();
        List<Long> tempList = categorys.stream().map(Category::getId).collect(Collectors.toList());
        for (Iterator<Category> iterator = categorys.iterator(); iterator.hasNext(); ) {
            Category category = iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(category.getParentId())) {
                recursionFn(categorys, category);
                returnList.add(category);
            }
        }
        if (returnList.isEmpty()) {
            returnList = categorys;
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<Category> list, Category t) {
        // 得到子节点列表
        List<Category> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Category tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<Category> getChildList(List<Category> list, Category t) {
        List<Category> tlist = new ArrayList<>();
        Iterator<Category> it = list.iterator();
        while (it.hasNext()) {
            Category n = it.next();
            if (n.getParentId().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<Category> list, Category t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
